﻿using ConfExpert.Forms.Base;
using ConfExpert.Utility;
using DevTeam;
using System.Data;
using System.Drawing;

namespace ConfExpert.Forms.Destex.Satis
{
    public partial class YeniSatis : XForm
    {
        public YeniSatis()
        {
            InitializeComponent();
        }

        private void YeniSatis_Load(object sender, System.EventArgs e)
        {
            //GetRates();

            gv.Init("DX_GetStok");
            gv.MakeColumnInvisible("ProductId", "Selling");
            gv.MakeReadOnly("QtyNew", "Fiyat");

            gv.AddColumn("FiyatTl", typeof(decimal), "Fiyat*Selling");

            /*                
                            "Fiyat* IIF(CurrencyId = 1, " + dictCur[1] + 
                            ", IIF(CurrencyId = 2, " + dictCur[2] +
                            ", IIF(CurrencyId = 3, " + dictCur[3] +
                            ", IIF(CurrencyId = 4, " + dictCur[4] +
                            ", IIF(CurrencyId = 5, " + dictCur[5] + ")))))");
                            */
            gv.AddColumn("ToplamTutarTl", typeof(decimal), "QtyNew*FiyatTl");
            gv.SetCaptions("QtyNew:Satılan mt.", "TopDetayi:Top Detayı", "Qty:Toplam (mt)", "HKQty:HK (mt)", "NetQty:Net (mt)", "MTMSatis:MTM Satış", "BulkSatis:Bulk Satış", "ToplamTutarTl:Toplam (₺)", "FiyatTl:Fiyat (₺)");
            gv.FormatAsNumber(2, "QtyNew", "FiyatTl", "ToplamTutarTl", "MTMSatis", "BulkSatis");
            gv.FormatAsNumber(1, "Qty", "HKQty", "NetQty");
            gv.AddSummary("QtyNew", "ToplamTutarTl", "Qty", "HKQty", "NetQty", "TopAdedi");

            //gv.SetCaptions("HKQty:HK.Qty", "NetQty:NetQty");
            gv.Columns["HKQty"].ToolTip = "Henüz Kesilmemiş Metraj";
            gv.Columns["NetQty"].ToolTip = "Kullanılabilir Metraj";

            gv.RowCellStyle += Gv_RowCellStyle;
            gv.BestFitColumns();
        }

        private void Gv_RowCellStyle(object sender, DevExpress.XtraGrid.Views.Grid.RowCellStyleEventArgs e)
        {
            switch (e.Column.FieldName)
            {
                case "QtyNew":
                case "Fiyat":
                    e.Appearance.BackColor = Color.LightYellow;
                    e.Appearance.FontStyleDelta = FontStyle.Bold;
                    //e.Appearance.ForeColor = Color.Green;
                    break;

                case "NetQty":
                    e.Appearance.FontStyleDelta = FontStyle.Bold;
                    break;
            }
        }

        private void btnKaydet_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            gv.EndEditing();
            DataTable dtSatis = gv.DataTable().GetChanges();
            if (dtSatis != null)
            {
                dtSatis.TableName = "DX_Satis";
                using (YeniSatisOnay satisOnay = new YeniSatisOnay(gv.DataTable()))
                {
                    if (satisOnay.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                    {
                        gv.RefreshDataSource("DX_GetStok");
                        gv.BestFitColumns();
                    }

                    AppSession.MainForm.ShowMdiChildForm(typeof(SiparisYonetimi), satisOnay.SatisId);
                }
            }
            else
                ShowInfoMessage("Lütfen satışı yapılan artikele miktar ve fiyat giriniz.");
        }
    }
}